package com.starblink.utils.app.vm

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.starblink.frame.base.BaseViewModel
import com.starblink.rocketreserver.FetchTopBrandFullListQuery
import androidx.lifecycle.viewModelScope
import com.apollographql.apollo3.api.Optional
import com.google.gson.Gson
import com.starblink.rocketreserver.CollectionProductMutation
import com.starblink.rocketreserver.FetchTopBrandSaleListQuery
import com.starblink.rocketreserver.type.TopBrandSaleListReq
import com.starblink.utils.log.SKLogger
import com.starblink.utils.network.base.SKGqlClient
import kotlinx.coroutines.launch

class MainVM : BaseViewModel() {

    val testResult: MutableLiveData<String> = MutableLiveData()

    fun reqQuery() {
        viewModelScope.launch {
            val response = SKGqlClient.skQuery(
                FetchTopBrandSaleListQuery(params = TopBrandSaleListReq(
                    pageNo = 1,
                    pageSize = 10,
                    filterType = 1,
                    sortType =  0
                ))
            )
            SKLogger.d("reqQuery","$response")
            testResult.value=Gson().toJson(response)
        }
    }

    fun reqQuery2() {
        viewModelScope.launch {
            val response = SKGqlClient.skQueryCacheAndNetwork(
//                FetchTopBrandFullListQuery()
                FetchTopBrandSaleListQuery(params = TopBrandSaleListReq(
                    pageNo = 1,
                    pageSize = 10,
                    filterType = 1,
                    sortType =  0
                ))
            )
            SKLogger.d("reqQuery2","$response")
            testResult.value=Gson().toJson(response)
        }
    }

    fun reqMutation() {
        viewModelScope.launch {
            val response = SKGqlClient.skMutation(
                CollectionProductMutation(
                    "071bbd639c514557522cf8356f8e0656", 1, outOfferId = Optional.present(null)
                )
            )
            SKLogger.d("reqMutation", "$response")
            testResult.value=Gson().toJson(response)
        }
    }
}